package cn.xjt.TcpFileLoadOptimized;

import java.io.*;
import java.net.Socket;

/**
 * @author xujiangtao
 * @create 2020-08-04-14:51
 */
public class ClientFileUpload {
    public static void main(String[] args) throws IOException {
        String str = null;

        Socket client = new Socket("192.168.0.112",9999);
        //得到一个网络输入流
        InputStream inputStream = client.getInputStream();
        //将读取到的输入流，发送到服务器
        OutputStream outputStream = client.getOutputStream();
        FileInputStream is =new FileInputStream(new File("E:\\图片\\H2vo2sM061.jpg"));
        //发送
        byte[] data = new byte[5000];
        int len=0;
        int count=0;
        System.out.println("开始发送数据");
        //通过字节流循环的发送数据，当len等于-1的时候，停止循环，发送成功！
        while ((len=is.read(data))!=-1)
        {
            System.out.println(len);
            count++;
            outputStream.write(data,0,len);     //将字节流发送，长度为0到len
            System.out.println(count);
        }
        //上传完文件，给服务器写一个结束标记
        client.shutdownOutput();        //禁用次套接字的输出流，结束了
        //回收
        byte[] upload = new byte[1024];
        int leng = inputStream.read(upload);
        System.out.println(new String(upload,0,leng));
    }
}
